package day08;

public class ThreadPrint {
    private static int number = 1;
    private static char letter = 'A';
    private static final Object lock = new Object();
    private static final int MAX_NUMBER = 26;
    private static final char MAX_LETTER = 'Z';

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (number <= MAX_NUMBER) {
                synchronized (lock) {
                    if (number <= MAX_NUMBER) {
                        System.out.print(number++);
                        lock.notify();
                        try {
                            if (number <= MAX_NUMBER) {
                                lock.wait();
                            }
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            while (letter <= MAX_LETTER) {
                synchronized (lock) {
                    if (letter <= MAX_LETTER) {
                        System.out.print(letter++);
                        lock.notify();
                        try {
                            if (letter <= MAX_LETTER) {
                                lock.wait();
                            }
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}
